Start the notebook server
open the notebook homepage
and create a new notebook.
In [2]:
print('hello world')
Add a new cell to the notebook: click the + button on the toolbar
Change the cell type using the drop down list in the toolbar or by using the ESC-M keyboard shortcut.
To "open" or select a markdown cell for editing, double click the cell.
View the rendered markdown by running the cell:
Markdown cells use markdown to generate formatted text:
Markdown can also show inline code
styles as well as code blocks:
def mycode():
''' Here is my non-executable code '''
pass
So what does the actual markdown look like?
In [ ]:
# Simple Markdown 1
Markdown cells use markdown to generate formatted text:
- inline styles
- *emphasise text*
- __strongly emphasise__ text
## Sub-headings
Markdown can also show `inline code` styles as well as code blocks:
````
def mycode():
''' Here is my non-executable code '''
pass
````
Markdown can include weblinks, eg to Data Carpentry, as well as links to named elements in the same notebook, or other notebooks.
Markdown can embed images:
So how do we do that?
In [ ]:
<a name=""></a>
# Simple Markdown 2
Markdown can include weblinks, eg to [Data Carpentry](https://datacarpentry.org), as well as links to named elements [in the same notebook](#navlink), or [other notebooks](path/example.ipynb#exampleNavlink).
Markdown can embed images:
[Jupyter logo](./jupyter-logo.png)
Mathematical expessions can be rendered inline by wrapping a LaTeX expression (no spaces) with a $ either side.
$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$
is rendered inline: $e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$
Use $$
to render in the centre of a new line: $$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$
To select a cell, click on it. The selected cell will be surrounded by a box with the left hand side highlighted.
Move the selection focus to the cell above/below using the keyboard up/down arrow keys.
Select multiple adjacent cells using SHIFT-UP ARROW
or SHIFT-DOWN ARROW
Delete a cell by selecting it and:
Edit -> Delete cells
or ESC-X
Undelete the last deleted cell:
Edit -> Undo Delete cells
or ESC-Z
Reorder cells by:
Edit -> Move Cell Up
or Edit -> Move Cell Down
Edit - >Cut
or Edit->Paste Cells Above
or Edit->Paste Cells Below
Cut selected cells
then Paste selected cells
You can also copy selected cells from the toolbar, Edit -> Copy Cells
or ESC-C
.
In [4]:
a=1
b=2
a+b
Out[4]:
In [5]:
print(a)
In [8]:
#Run this cell multiple times
a=a+1
a
Out[8]:
In [13]:
import numpy as np
np.pi
Out[13]:
Code cells also act as a commandline prompt - prefix with a !
In [11]:
! ls *.ipynb # Linux / Mac;
#for windows: ! dir *.ipynb
Line numbering in code cells can be toggled with ESC-L.
In [16]:
import pandas as pd
pd.DataFrame({'col1':['x','y'],'col2':[1,2]})
Out[16]:
In [14]:
%matplotlib inline
import matplotlib.pyplot as plt
# Create 1000 evenly-spaced values from 0 to 2 pi
x = np.linspace(0, 2*np.pi, 1000)
#Plot a sine wave over those values
y = np.sin(x)
plt.plot(x, y)
#You can prevent the display of object details returned from the plot by:
## - adding a semi-colon (;) at the end of the final statement
Out[14]:
If you reate a function that accepts one or more parameters, you may be able to use it as the basis of an automatically generated application.
For example, suppose we have a function that will plot a sine wave over the range 0..2 pi for a specified frequency, passed into the function as a parameter:
In [18]:
#If no frequency value is specified, use the default setting: f=1
def sinplot(f=1):
#Define a range of x values
x = np.linspace(0, 2*np.pi, 1000)
#Plot a sine wave with the specified frequency over that range
y = np.sin(f*x)
#Plot the chart
plt.plot(x, y)
sinplot(f=3)
In [20]:
from ipywidgets import interact
interact(sinplot, f=5)
Out[20]:
You can also specify the range of values Applied to an interact()
slider:
interact(sinplot, f=[0,20])
Or the range of values and the step size:
interact(sinplot, f=[0,20,5])
Run multiple cells:
Cells -> Run All Above
Cells -> Run All Below
Cells -> Run All
Run cells from scratch (i.e. from a fresh kernel), Kernel -> Restart and Clear Output
and then run the cells you want.
To run all the cells in the notebook from scratch: Kernel -> Restart and Run All
Code cells support autocomplete: start typing and then TAB
to see what options are available...
Access documentation for a function - add a ?
and run the cell:
In [ ]:
pd.DataFrame?
Code cells that are running (or queued for running) display an asterisk in the cell In []
indicator.
To stop execution of a running cell (and prevent queued cells from executing):
Kernel -> Interrupt
If the notebook is still hanging, you may need to restart the kernel: Kernel -> Restart
This slide deck was produced from a notebook styled as a slideeck:
View -> Cell Toolbar -> Slideshow
, and then either:jupyter nbconvert slideTest.ipynb --to slides --post serve
, orjupyter nbconvert slideTest.ipynb --to slides && python -m SimpleHTTPServer 8000
#then go to localhost:8000 in browser